package cn.tedu.fresheveryday.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@SuppressWarnings("ALL")
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    // 指定Controller包路径
    private String basePackage = "cn.tedu.fresheveryday.controller";
    // 主机名
    private String host = "http://localhost:8081";
    // 标题
    private String title = "生鲜超市";
    // 简介
    private String description = "小型生鲜超市";
    // 服务条款URL
    private String termsOfServiceUrl = "http://localhost:8081";
    // 作者信息
    private String contactName = "胡永川";
    // 作者网址
    private String contactUrl = "http://localhost:8081";
    // 联系邮箱
    private String contactEmail = "2365264585@qq.com";
    // 版本号
    private String version = "1.0";

    @Autowired
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket docket() {
        String groupName = "1.0.0";

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .host(host)
                .apiInfo(apiInfo())
                .groupName(groupName)
                //是否启用
                .enable(true)
                .select()
                //配置要扫描接口的方式
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                //过滤
                .paths(PathSelectors.any())
                .build()
                .extensions(openApiExtensionResolver.buildExtensions(groupName));
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }

}
